package com.ruoyi.pcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.mashuai.utils.base.MsAssertUtils;
import com.mashuai.utils.toolkit.core.msConverts.MsStreamUtils;
import com.mashuai.utils.toolkit.core.msHandlebean.annotation.MsSaveCheck;
import com.ruoyi.basis.service.MsBaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

import com.mashuai.utils.toolkit.core.msMp.MsDb;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.basis.utils.mp.MpConditionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pcj.constant.TipConstant;
import com.ruoyi.pcj.domain.PcjHobby;
import com.ruoyi.pcj.dto.match.MatchUserAttrDto;
import com.ruoyi.pcj.utils.pcj.PcjUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

import com.ruoyi.pcj.mapper.PcjUserHobbyMapper;
import com.ruoyi.pcj.domain.PcjUserHobby;
import com.ruoyi.pcj.service.IPcjUserHobbyService;

import java.util.Map;

import com.ruoyi.basis.utils.mp.MpPageUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户爱好Service业务层处理
 *
 * @author ms
 * @date 2024-03-12
 */
@Service
public class PcjUserHobbyServiceImpl extends MsBaseServiceImpl<PcjUserHobbyMapper, PcjUserHobby> implements IPcjUserHobbyService {

    @Override
    public <E extends IPage<PcjUserHobby>> E page(E page, Wrapper<PcjUserHobby> qw) {
        return baseMapper.selectPage(page, qw);
    }

    /**
     * 查询用户爱好列表
     *
     * @param params 聊天室-查询参数 {"pageNum":1,"pageSize":10}
     * @return 用户爱好
     */
    @Override
    public IPage<PcjUserHobby> selectPcjUserHobbyList(Map<String, Object> params) {
        //获取条件构造器
        QueryWrapper<PcjUserHobby> qw = MpConditionUtils.getQueryWrapper(params, PcjUserHobby.class);
        //分页查询
        Page<PcjUserHobby> page = this.page(MpPageUtils.getPage(params), qw);
        return page;
    }


    /**
     * 根据id查询用户爱好
     *
     * @param id 用户爱好主键
     * @return 用户爱好
     */
    @Override
    public PcjUserHobby selectPcjUserHobbyById(String id) {
        //首先获取主表信息
        PcjUserHobby pcjUserHobby = MsDb.getById(id, PcjUserHobby.class);
        return pcjUserHobby;
    }

    /**
     * 回填子表信息
     **/

    /**
     * 新增用户爱好
     *
     * @param pcjUserHobby 用户爱好
     * @return 结果
     */
    @Override
    public Boolean insertPcjUserHobby(PcjUserHobby pcjUserHobby) {
        //插入主表
        Boolean success = this.insertData(pcjUserHobby);
        return success;
    }

    /**
     * 修改用户爱好
     *
     * @param pcjUserHobby 用户爱好
     * @return 结果
     */
    @Override
    public Boolean updatePcjUserHobby(PcjUserHobby pcjUserHobby) {
        //修改主表
        Boolean success = this.updateData(pcjUserHobby);
        return success;
    }

    /**
     * 批量删除用户爱好
     *
     * @param ids 需要删除的用户爱好主键
     * @return 结果
     */
    @Override
    public Boolean deletePcjUserHobbyByIds(String[] ids) {

        //删除主表
        Boolean success = this.removeBatchByIds(Arrays.asList(ids));
        return success;
    }

    @Override
    public List<PcjUserHobby> getUserHobby() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<PcjUserHobby> qw = Wrappers.lambdaQuery();
        qw.eq(PcjUserHobby::getUserId, userId);
        List<PcjUserHobby> userHobbyList = baseMapper.selectList(qw);
        return userHobbyList;
    }


    /**
     * 根据主表id，删除子表所有数据
     **/
    /**
     * 对外提供的设置默认字段值，id为空新增，不为空更新
     **/
    public void setEntityDefValAuto(PcjUserHobby pcjUserHobby) {
        this.setDefFieldVal(pcjUserHobby);
    }

    @Override
    public Map<Long, List<PcjUserHobby>> getUserHobbyData(List<String> hobbyIds, List<Long> matchTargetUserIds) {
        LambdaQueryWrapper<PcjUserHobby> qw = Wrappers.lambdaQuery();
        qw.in(PcjUserHobby::getHobbyId, hobbyIds);
        qw.in(PcjUserHobby::getUserId, matchTargetUserIds);
        List<PcjUserHobby> data = baseMapper.selectList(qw);
        Map<Long, List<PcjUserHobby>> dataMap = MsStreamUtils.groupByLink(data, PcjUserHobby::getUserId);
        return dataMap;
    }

    @Override
    public List<String> getUserHobbyIds(Long userId, Map<String, PcjHobby> allHobbyMap) {
        //当前用户所有爱好
        List<PcjUserHobby> userHobbyList = PcjUtils.getDataByCurUserSeqNoAsc(userId, PcjUserHobby.class);
        MsAssertUtils.objIsNull(userHobbyList, TipConstant.MATCH_NO_SET_HOBBY);
        List<String> userHobbyIds = MsStreamUtils.listStreamMap(userHobbyList, PcjUserHobby::getHobbyId);
        return userHobbyIds;
    }

    @Override
    public List<MatchUserAttrDto> getEqualsHobbyUserMsg(List<String> userHobbyIds, List<MatchUserAttrDto> equalsTargetUserList) {
        //基于目标一致的用户，进行兴趣二次筛选。筛选出兴趣与当前匹配用户一致的用户
        LambdaQueryWrapper<PcjUserHobby> qw = Wrappers.lambdaQuery();
        qw.in(PcjUserHobby::getHobbyId, userHobbyIds);
        qw.in(PcjUserHobby::getUserId, MsStreamUtils.listStreamMap(equalsTargetUserList, MatchUserAttrDto::getUserId));
        List<PcjUserHobby> hobbyList = baseMapper.selectList(qw);
        MsAssertUtils.objIsNull(hobbyList, TipConstant.MATCH_NO_HOBBY);
        List<MatchUserAttrDto> equalsHobbyUserList = new ArrayList<>();
        for (PcjUserHobby t : hobbyList) {
            equalsHobbyUserList.add(new MatchUserAttrDto(t.getUserId(), t.getHobbyId()));
        }
        return equalsHobbyUserList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveUserHobby(List<PcjUserHobby> hobbyList) {
        //先删除所有的，在插入
        Long userId = SecurityUtils.getUserId();
        baseMapper.realDeleteUserHobby(userId);
        //重新插入
        for (PcjUserHobby hobby : hobbyList) {
            setEntityDefValAuto(hobby);
        }
        this.saveOrUpdateBatch(hobbyList);
    }

    @Override
    public List<String> getCurUserHobbyIds(Long userId) {
        //当前用户所有爱好
        List<PcjUserHobby> userHobbyList = PcjUtils.getDataByUserId(userId, PcjUserHobby.class);
        MsAssertUtils.objIsNull(userHobbyList, TipConstant.MATCH_NO_SET_HOBBY);
        List<String> userHobbyIds = MsStreamUtils.listStreamMap(userHobbyList, PcjUserHobby::getHobbyId);
        return userHobbyIds;
    }

    @Override
    public List<MatchUserAttrDto> getAccordHobbyUserIds(List<MatchUserAttrDto> accordTargetUserMsg, List<String> curUserHobbyIds) {
        LambdaQueryWrapper<PcjUserHobby> qw = Wrappers.lambdaQuery();
        qw.in(PcjUserHobby::getUserId, MsStreamUtils.listStreamMap(accordTargetUserMsg, MatchUserAttrDto::getUserId));
        qw.in(PcjUserHobby::getHobbyId, curUserHobbyIds);
        List<PcjUserHobby> data = baseMapper.selectList(qw);
        MsAssertUtils.objIsNull(data, TipConstant.MATCH_NO_HOBBY);
        List<MatchUserAttrDto> accordHobbyUserMsg = new ArrayList<>();
        for (PcjUserHobby hobby : data) {
            accordHobbyUserMsg.add(new MatchUserAttrDto(hobby.getUserId(),hobby.getHobbyId()));
        }
        return accordHobbyUserMsg;
    }
}
